package com.pleague.service;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.pleague.common.utils.StringUtils;
public class BaseService {
	protected Logger LOG = LoggerFactory.getLogger(this.getClass());
	protected void logInfo(String s) {
		LOG.info(s);
	}
	
	protected void logInfo(String s, Object... arr) {
		// 时间：%s,1ms = 时间：1ms
		String format = StringUtils.formatString(s, arr);
		LOG.info(format);
	}

	protected void logWarn(String s) {
		// 时间：%s,1ms = 时间：1ms
		LOG.warn(s);
	}

	protected void logWarn(String s, Object... arr) {
		// 时间：%s,1ms = 时间：1ms
		String format = StringUtils.formatString(s, arr);
		LOG.warn(format);
	}

	protected void logError(String s, Exception e) {
		LOG.error(s, e);
	}

	protected void logError(String s, Exception e, Object... arr) {
		String format = StringUtils.formatString(s, arr);
		LOG.error(format, e);
	}

	public String readInputStream(InputStream in, String encoding) {
		String sCurrentLine = "";
		StringBuilder sbu = new StringBuilder();
		try {
			if (in == null) {
				return "";
			}
			BufferedReader buffer = new BufferedReader(new InputStreamReader(in, encoding));
			while ((sCurrentLine = buffer.readLine()) != null) {
				if (!StringUtils.isEmpty(sCurrentLine)) {
					sbu.append(sCurrentLine);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logError("读取inputStream数据出错", e);
		} finally {
			try {
				if (in != null)
					in.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return sbu.toString();
	}

	public InputStream getInputStream(String fileName) throws Exception {
		// String path = Thread.currentThread().getContextClassLoader()
		// .getResource("").getPath();
		String path = BaseService.class.getResource("/").getPath();
		String fp = path + fileName;
		logInfo(fp);
		File file = new File(fp);
		if (file.isFile()) {
			logInfo(fp + "文件存在!");
			FileInputStream in = new FileInputStream(file);
			return in;
		} else {
			logInfo("BaseService.class.getResourceAsStream(\"/" + fileName + "\")");
			InputStream in = BaseService.class.getResourceAsStream("/" + fileName);
			return in;
		}
	}

}
